package com.hnnou.freshmenappserver.controller;

import com.hnnou.freshmenappserver.bean.News;
import com.hnnou.freshmenappserver.bean.User;
import com.hnnou.freshmenappserver.service.NewsService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "新闻接口")

public class NewsController {
    @Autowired
    NewsService newsService;
    @PostMapping("/news/list")
    @ResponseBody
    public Map<String, Object> list() {
        Map<String, Object> map = new HashMap<>();
        List<News> list = newsService.findAll();
        if (list != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("list",list);

        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

    @PostMapping("/news/findone")
    @ResponseBody
    public Map<String, Object> findOneById(@RequestBody News news) {
        Map<String, Object> map = new HashMap<>();
        News news1 = newsService.selectById(news.getId());
        if (news1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", news1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
    @PostMapping("/news/delete")
    @ResponseBody
    public Map<String, Object> delete(@RequestBody News news) {
        Map<String, Object> map = new HashMap<>();
        int result = newsService.deleteById(news.getId());
        if(result>0){

            map.put("success", true);
            map.put("msg", "成功");
        } else{
            map.put("success", false);
            map.put("msg", "失败");
        }

        return map;
    }
    @PostMapping("/news/update")
    @ResponseBody
    public Map<String, Object> update(@RequestBody News news) {
        Map<String, Object> map = new HashMap<>();
        int count  = newsService.update(news);
        if (count > 0) {
            map.put("success", true);
            map.put("msg", "成功");
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
    // 添加
    @PostMapping("/news/addone")
    @ResponseBody
    public Map<String, Object> insert(@RequestBody News news) {
        Map<String, Object> map = new HashMap<>();
        int count = newsService.insert(news);
        if (count > 0) {
            map.put("success", true);
            map.put("msg", "成功");
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

}
